package thread.masterworker;

import cn.hutool.core.thread.ThreadUtil;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

// 省略import
public class MasterWorkerTest {
    //简单任务
    static class SimpleTask extends Task<Integer> {
        @Override
        protected Integer doExecute() {
            System.out.println("task " + getId() + " is done ");
            return getId();
        }
    }

    public static void main(String[] args) {
        //创建Master，包含4个Worker，并启动Master的执行线程
        Master<SimpleTask, Integer> master = new Master<>(4);

        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
        scheduledExecutorService.schedule(
                () -> master.submit(new SimpleTask()),
                2, TimeUnit.SECONDS
        );

        scheduledExecutorService.schedule(
                () -> master.printResult(),
                5, TimeUnit.SECONDS
        );

//        //定期向master提交任务
//        ThreadUtil.scheduleAtFixedRate(() -> master.submit(
//                new SimpleTask()),
//                2, TimeUnit.SECONDS);
//
//        //定期从master提取结果
//        ThreadUtil.scheduleAtFixedRate(
//                () -> master.printResult(),
//                5, TimeUnit.SECONDS);

    }
}